package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.OrderInfo;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.OrderInfoService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;
import cn.piaoyi.web.ui.OptionsString;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminOrderInfo extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminOrderInfo.class);
	
	private OrderInfoService orderInfoService;

	public OrderInfoService getOrderInfoService() {
		return orderInfoService;
	}

	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}
	
	private List orderStatusList = new ArrayList();
	private List shippingStatusList = new ArrayList();
	private List payStatusList = new ArrayList();

	public List getOrderStatusList() {
		return orderStatusList;
	}

	public void setOrderStatusList(List orderStatusList) {
		this.orderStatusList = orderStatusList;
	}

	public List getShippingStatusList() {
		return shippingStatusList;
	}

	public void setShippingStatusList(List shippingStatusList) {
		this.shippingStatusList = shippingStatusList;
	}

	public List getPayStatusList() {
		return payStatusList;
	}

	public void setPayStatusList(List payStatusList) {
		this.payStatusList = payStatusList;
	}

	private void setOrderStatusListValues(){
		//订单状态。0，未确认；1，已确认；2，己付款；3，己发货；4，己完成；
		orderStatusList.add(new OptionsInt(-1, "请选择"));
		orderStatusList.add(new OptionsInt(0, "未付款"));
		orderStatusList.add(new OptionsInt(1, "己付款"));
		orderStatusList.add(new OptionsInt(2, "己发货"));
		orderStatusList.add(new OptionsInt(3, "己完成"));
	}
	private void setShippingStatusListValues(){
		//商品配送情况，0，未发货；1，已发货；
		shippingStatusList.add(new OptionsInt(-1, "请选择"));
		shippingStatusList.add(new OptionsInt(0, "未发货"));
		shippingStatusList.add(new OptionsInt(1, "已发货"));
	}
	
	private void setPayStatusListValues(){
		//支付状态；0，未付款；1，已付款'
		payStatusList.add(new OptionsInt(-1, "请选择"));
		payStatusList.add(new OptionsInt(0, "未付款"));
		payStatusList.add(new OptionsInt(1, "已付款"));
	}
	
	private void initValues(){
		setOrderStatusListValues();
		setShippingStatusListValues();
		setPayStatusListValues();
	}


	private OrderInfo orderInfo;	//订单信息
	private long id;			//主键ID
	private int orderStatus = -1;	//订单状态
	private int shippingStatus = -1;	//商品配送情况
	private int payStatus = -1;		//支付状态
	private String shippingNO;	//配送号
	
	public OrderInfo getOrderInfo() {
		return orderInfo;
	}

	public void setOrderInfo(OrderInfo orderInfo) {
		this.orderInfo = orderInfo;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(int orderStatus) {
		this.orderStatus = orderStatus;
	}

	public int getShippingStatus() {
		return shippingStatus;
	}

	public void setShippingStatus(int shippingStatus) {
		this.shippingStatus = shippingStatus;
	}

	public int getPayStatus() {
		return payStatus;
	}

	public void setPayStatus(int payStatus) {
		this.payStatus = payStatus;
	}

	public String getShippingNO() {
		return shippingNO;
	}

	public void setShippingNO(String shippingNO) {
		this.shippingNO = shippingNO;
	}
	
	
	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		initValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_orderinfo.action?orderStatus=" + this.orderStatus + "&shippingStatus=" + this.shippingStatus + "&payStatus=" + this.payStatus);
		this.setPageList(this.getOrderInfoService().findOrderInfos(-1, orderStatus, shippingStatus, payStatus,"id", 1, pages));
		
		return SUCCESS;
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		this.orderInfo = this.getOrderInfoService().findOrderInfoById(this.id);
		this.orderStatus = orderInfo.getOrderStatus();
		this.shippingStatus = orderInfo.getShippingStatus();
		this.payStatus = orderInfo.getPayStatus();
		this.shippingNO = orderInfo.getShippingNO();
		return INPUT;
	}
	
	public String doupdate(){
		//更新
		this.orderInfo = this.getOrderInfoService().findOrderInfoById(this.id);
		orderInfo.setOrderStatus(this.orderStatus);
		orderInfo.setShippingStatus(this.shippingStatus);
		orderInfo.setPayStatus(this.payStatus);
		orderInfo.setShippingNO(this.shippingNO);
		try {
			this.getOrderInfoService().updateOrderInfo(orderInfo);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}



}
